<template>
  <el-container>
    <el-header id="header" class="navbar navbar-inverse navbar-fixed-top">
      <nav class="container">
        <div class="navbar-collapse">
          <a href="/">
            <div class="navbar-brand">Submit</div>
          </a>
          <el-menu
            :default-active="activeIndex2"
            class="el-menu-demo"
            mode="horizontal"
            @select="handleSelect"
            text-color="#fff"
            active-text-color="#e74c3c"
            background-color="#3d4450"
          >
            <el-submenu index="1">
              <template slot="title">任务管理</template>
              <el-menu-item index="1-1">任务列表</el-menu-item>
              <el-menu-item index="1-2">新建任务</el-menu-item>
            </el-submenu>
            <el-submenu index="2">
              <template slot="title">提交人管理</template>
              <el-menu-item index="2-1">提交人名单</el-menu-item>
              <el-menu-item index="2-2">新建提交人</el-menu-item>
            </el-submenu>
            <el-submenu index="3">
              <template slot="title">标签管理</template>
              <el-menu-item index="3-1">新建标签</el-menu-item>
            </el-submenu>
            <el-submenu index="4">
              <template slot="title">关于submit</template>
              <el-menu-item index="4-1">组员简介</el-menu-item>
            </el-submenu>
          </el-menu>
        </div>
      </nav>
    </el-header>
    <el-main id="boder">
      <div class="container">
        <div class="row">
          <el-col id="mainstay" :span="8">
            <div class="ct">
              <div class="mn" style="margin-top: 20px; background: #fff">
                <div
                  class="main-hnav"
                  style="padding: 10px; border-bottom: 1px solid #eeeeee"
                >
                  <h3 id="crtask" style="font-size: 16px">
                    创建任务
                    <em
                      style="
                        font-family: 'Lucida Sans Unicode', 'Lucida Grande',
                          sans-serif;
                        font-size: 12px;
                        color: #999999;
                        padding-left: 5px;
                      "
                      >/create</em
                    >
                  </h3>
                </div>
                <el-form
                  ref="form"
                  :model="form"
                  label-width="100px"
                  style="margin: 50px"
                  :rules="rules"
                >
                  <div class="layer_cr">
                    <div class="mtw">
                      <div id="crinfo">
                        <el-form-item label="任务名称" prop="title">
                          <el-input v-model="form.title"></el-input>
                        </el-form-item>
                        <el-form-item label="任务描述" prop="description">
                          <el-input
                            type="textarea"
                            v-model="form.description"
                          ></el-input>
                        </el-form-item>
                        <el-form-item label="选择提交人" prop="submitters">
                          <el-button
                            type="text"
                            @click="visibleTable"
                            class="selectButton"
                            >选择</el-button
                          >
                          <el-dialog
                            title="提交人名单"
                            :visible.sync="dialogTableVisible"
                          >
                            <div class="tagList">
                              <el-col
                                v-for="(item, index) in tags"
                                :key="item"
                                :offset="index / index"
                                :span="2"
                              >
                                <span
                                  class="tag sub_tag"
                                  :class="[
                                    selectTags[index] == 1
                                      ? 'tag_active'
                                      : 'tag_unactive',
                                  ]"
                                >
                                  <i class="el-icon-collection-tag"></i>
                                  <a
                                    href="#"
                                    @click.prevent="selectTag(index)"
                                    >{{ item.value }}</a
                                  >
                                </span>
                              </el-col>
                            </div>
                            <el-table
                              :data="tableData"
                              tooltip-effect="dark"
                              style="width: 100%"
                              @selection-change="handleSelectionChange"
                            >
                              <el-table-column type="selection" min-width="10%">
                              </el-table-column>
                              <el-table-column
                                prop="id"
                                label="id"
                                min-width="45%"
                              >
                              </el-table-column>
                              <el-table-column
                                prop="name"
                                label="姓名"
                                min-width="45%"
                              >
                              </el-table-column>
                            </el-table>
                            <div class="block" style="text-align: center">
                              <el-pagination
                                @current-change="getPageData"
                                :current-page="pageObject.pageNum"
                                :page-size="pageObject.pageSize"
                                layout="total, prev, pager, next, jumper"
                                :total="pageObject.total"
                              >
                              </el-pagination>
                            </div>
                            <div class="crinfo-btn">
                              <el-button
                                class="btn btn-inverse-primary"
                                @click="hideTable"
                                type="primary"
                                >确认</el-button
                              >
                            </div>
                          </el-dialog>
                        </el-form-item>

                        <el-form-item label="起止时间" required>
                          <el-form-item prop="stime">
                            <el-date-picker
                              type="datetime"
                              placeholder="选择日期"
                              format="yyyy-MM-dd HH:mm:ss"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              v-model="form.createTime"
                              style="width: 100%"
                              :picker-options="beginOptions"
                            ></el-date-picker>
                          </el-form-item>
                          -
                          <el-form-item prop="etime">
                            <el-date-picker
                              type="datetime"
                              placeholder="选择日期"
                              format="yyyy-MM-dd HH:mm:ss"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              v-model="form.deadline"
                              style="width: 100%"
                              :picker-options="endOptions"
                            ></el-date-picker>
                          </el-form-item>
                        </el-form-item>
                      </div>
                    </div>
                  </div>
                  <div class="crinfo-btn">
                    <el-button
                      class="btn btn-inverse-primary"
                      @click="onSubmit"
                      type="primary"
                      >提交</el-button
                    >
                  </div>
                </el-form>
              </div>
            </div>
          </el-col>

          <el-col id="sidebar" :span="4">
            <aside id="task-login">
              <div class="panel task-panel">
                <div class="panel-heading">
                  <i class="el-icon-sort-up"></i>
                  欢迎!
                  <i class="el-icon-sort-down"></i>
                </div>
                <div class="login-panel text-center">
                  <img
                    src="https://cache.cswsadlab.com/wp-content/uploads/2015/03/笑颜1-150x150.jpg"
                    alt="头像"
                    class="avatar avatar-60 wp-user-avatar wp-user-avatar-60 photo avatar-default"
                    width="60"
                    height="60"
                  />
                  <a class="user-name" href="https://vcb-s.com/wp-admin/"
                    >kamike</a
                  >
                  <a
                    class="btn btn-inverse-primary"
                    href="#"
                    @click.prevent="logout"
                    title="退出登录"
                    >退出登录</a
                  >
                </div>
              </div>
            </aside>
          </el-col>
        </div>
      </div>
    </el-main>
    <el-footer id="task-footer">
      <div class="container">
        我们的目标是征服星河大海 Powered By
        <a href="/test" target="_blank">Submit</a>
      </div>
    </el-footer>
  </el-container>
</template>

<script>
import { createTask } from "@/api/brand.js";
import { filterSub } from "@/api/brand.js";
import { listAllSub } from "@/api/brand.js";
import { formatDate } from "@/utils/dateformat.js";
import { tagList } from "@/api/brand.js";
import { subList } from "@/api/brand.js";
import { subTagList } from "@/api/brand.js";
import { isTaskFileExist } from "@/api/brand.js";
import { getToken } from "@/utils/auth";

const defaultListInsert = {
  title: null,
  description: null,
  word: null,
  createTime: null,
  deadline: null,
  fcount: null,
  content: null,
};
const pageParam = {
  list: [],
  pageNum: null,
  pageSize: null,
  total: null,
  totalPage: null,
};
export default {
  data() {
    var validateSubmitter = (rule, value, callback) => {
      if (this.multipleSelection.length === 0)
        callback(new Error("提交人为空"));
    };
    return {
      form: Object.assign({}, defaultListInsert),
      tableData: [],
      multipleSelection: [],
      dialogTableVisible: false,
      fileList: [],
      tid: null,
      list: [],
      cid: null,
      tags: [],
      activeIndex: "1",
      activeIndex2: "1",
      tag: null,
      curTag: "",
      headers: {},
      selectTags: [],
      searchTags: [],
      currentPage: 1,
      pageObject: null,
      beginOptions: {
        disabledDate: (time) => {
          if (this.form.deadline) {
            return (
              time.getTime() >= new Date(this.form.deadline).getTime() &&
              time.getTime() <= new Date()
            );
          } else {
            return time.getTime() <= new Date();
          }
        },
      },
      endOptions: {
        disabledDate: (time) => {
          if (this.form.createTime)
            return time.getTime() <= new Date(this.form.createTime).getTime();
        },
      },

      rules: {
        title: [
          { required: true, message: "请输入任务名称", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        description: [
          { required: true, message: "请输入任务描述", trigger: "blur" },
          {
            min: 3,
            max: 100,
            message: "长度在 3 到 100 个字符",
            trigger: "blur",
          },
        ],
        submitters: [
          //{ required: true, message: "请选择提交人", trigger: "change" },
          { validator: validateSubmitter, trigger: "change" },
        ],
        stime: [
          {
            type: "date",
            required: true,
            message: "请选择起始时间",
            trigger: "change",
          },
        ],
        etime: [
          {
            type: "date",
            required: true,
            message: "请选择截止时间",
            trigger: "change",
          },
        ],
      },
    };
  },
  created() {
    this.headers.Authorization = getToken();
    this.cid = this.$route.query.cid;
    this.searchTags.push("所有");
    this.pageObject = Object.assign({}, pageParam);
    this.getPageData(1);
    this.getTagList();
  },
  methods: {
    logout() {
      this.$store.dispatch("LogOut").then((response) => {});
      this.$router.push({
        path: "/login",
      });
    },
    getPageData(val) {
      console.log(`当前页: ${val}`);
      this.tableData.splice(0, this.tableData.length);
      var params = new URLSearchParams();
      params.append("cid", this.cid);
      params.append("tags", this.searchTags);
      params.append("currentPage", val);
      params.append("pageSize", "");
      subList(params).then((response) => {
        let data = response.data;
        this.pageObject.list = data.list;
        this.pageObject.pageNum = data.pageNum;
        this.pageObject.pageSize = data.pageSize;
        this.pageObject.total = data.total;
        this.pageObject.totalPage = data.totalPage;
        for (var i = 0; i < this.pageObject.list.length; i++) {
          let item = {};
          item.id = this.pageObject.list[i].id;
          item.name = this.pageObject.list[i].name;
          item.phone = this.pageObject.list[i].phone;
          item.wechat = this.pageObject.list[i].wechat;
          this.tableData.push(item);
        }
      });
    },
    selectTag(index) {
      this.selectTags[index] = this.selectTags[index] == 1 ? 0 : 1;
      console.log(this.selectTags[index]);
      //console.log(this.selectTags[index]);
      this.searchTags.splice(this.searchTags.indexOf(this.tags[index].value));
      if (this.selectTags[index] == 1)
        this.searchTags.push(this.tags[index].value);

      this.getPageData(this.currentPage);
    },
    quit() {
      this.$router.push({
        path: "/login",
      });
    },
    handleSelect(key, keyPath) {
      console.log(key, keyPath);
      if (keyPath[1] === "1-2") {
        this.$router.push({
          path: "/crtask",
          query: { cid: this.cid },
        });
      } else if (keyPath[1] === "1-1") {
        this.$router.push({
          path: "/coltask",
          query: { cid: this.cid },
        });
      } else if (keyPath[1] === "2-1") {
        this.$router.push({
          path: "/colsub",
          query: { cid: this.cid },
        });
      } else if (keyPath[1] === "2-2") {
        this.$router.push({
          path: "/crsub",
          query: { cid: this.cid },
        });
      } else if (keyPath[1] === "3-1") {
        this.$router.push({
          path: "/crtag",
          query: { cid: this.cid },
        });
      }
    },
    getTagList() {
      tagList(this.cid).then((response) => {
        let data = response.data;
        for (var i = 0; i < data.length; i++) {
          let item = {};
          item.text = data[i];
          item.value = data[i];
          this.tags.push(item);
        }
      });
    },
    visibleTable() {
      this.dialogTableVisible = true;
    },
    hideTable() {
      this.dialogTableVisible = false;
    },
    onSubmit() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.word = "";
          this.form.fcount = 0;
          this.form.content = "";
          createTask(this.cid, this.form).then((response) => {
            this.tid = response.data;
            console.log(this.tid);
            let params = new URLSearchParams();
            params.append("tid", this.tid);
            let selection = [];
            for (var i = 0; i < this.multipleSelection.length; i++) {
              selection.push(this.multipleSelection[i].id);
            }
            params.append("sids", selection);
            filterSub(params);
            this.$message.warning("创建成功");
            this.$router.push({
              path: "/coltask",
              query: {
                cid: this.cid,
              },
            });
          });
        } else {
          return false;
        }
      });
    },

    handleSelectionChange(val) {
      this.multipleSelection = val;
      console.log(this.multipleSelection);
    },
  },
};
</script>

<style scoped>
.tag_active {
  background-color: #409eff;
}

.tag_unactive {
  background-color: #3d4450;
}

.sub_tag a {
  color: #ecf0f1;
}

.tag {
  margin-right: 5px;
  font-size: 90%;
  display: inline;
  padding: 0.2em 0.6em 0.3em;
  font-weight: bold;
  line-height: 1;
  color: #ffffff;
  text-align: center;
  white-space: nowrap;
  vertical-align: baseline;
  border-radius: 0.25em;
}
#task-footer {
  display: block;
  text-align: center;
  color: #ecf0f1;
  padding: 50px;
  background-color: #3d4450;
}
#task-footer a {
  color: #ecf0f1;
}

#task-footer {
  display: block;
  text-align: center;
  color: #ecf0f1;
  padding: 50px;
  background-color: #3d4450;
}
#task-footer a {
  color: #ecf0f1;
}

#sidebar aside > div {
  border-radius: 4px;
  box-shadow: 1px -1px 6px rgba(155, 89, 182, 0.5);
  -webkit-box-shadow: 1px -1px 6px rgba(155, 89, 182, 0.5);
  background-color: white;
}

.panel {
  margin-bottom: 20px;
  border: 1px solid transparent;
}

.panel-heading {
  border-bottom: none;
  font-size: 14px;
  line-height: 14px;
  font-weight: normal;
}
.panel-heading {
  padding: 10px 15px;
  border-bottom: 1px solid transparent;
  border-top-right-radius: 3px;
  border-top-left-radius: 3px;
}
.login-panel {
  height: 100px;
  padding: 20px 0px;
}
.text-center {
  text-align: center;
}
.login-panel img {
  margin-right: 20px;
  border-radius: 50%;
  border: 2px solid white;
  background-color: white;
  -webkit-transition: all 0.3s ease;
  -moz-transition: all 0.3s ease;
  -o-transition: all 0.3s ease;
  transition: all 0.3s ease;
}
img {
  max-width: 100%;
  border-radius: 4px;
  vertical-align: middle;
  height: auto;
}
.login-panel .user-name {
  margin-right: 20px;
}
.btn-inverse-primary {
  color: white !important;
  background-color: #3d4450;
  border-color: #3d4450;
  -webkit-transition: all 0.3s ease;
  -moz-transition: all 0.3s ease;
  -o-transition: all 0.3s ease;
  transition: all 0.3s ease;
}
.task-panel a {
  color: #666666;
}
.btn {
  display: inline-block;
  padding: 6px 12px;
  margin-bottom: 0;
  font-size: 12px;
  font-weight: normal;
  line-height: 1.428571429;
  text-align: center;
  white-space: nowrap;
  vertical-align: middle;
  cursor: pointer;
  border: 1px solid transparent;
  border-radius: 4px;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  -o-user-select: none;
  user-select: none;
}
.btn-danger {
  color: #ffffff;
  background-color: #e74c3c;
  border-color: #e74c3c;
  transition: all 0.25s ease-in-out;
  -webkit-transition: all 0.25s ease-in-out;
  -moz-transition: all 0.25s ease-in-out;
  -o-transition: all 0.25s ease-in-out;
}
.el-header {
  background-color: #3d4450;
  border-color: #080808;
  top: 0;
  z-index: 1030;
}
#header {
  display: fixed;
  padding: 0;
  min-height: 50px;
}

.container {
  max-width: 1170px;
  margin-right: auto;
  margin-left: auto;
}

.el-menu {
  background-color: #3d4450;
  font-size: 14px;
  display: block;
}

.el-menu::after,
.el-menu::before {
  display: none;
}

.navbar-brand {
  margin-left: -15px;
  width: 200px;
  height: 50px;
  display: block;
  float: left;
  font-size: 40px;
  color: white;
  text-align: center;
}

.navbar-collapse {
  margin-left: -15px;
  margin-right: -15px;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.1);
}

#boder {
  padding: 30px 0;
}

.container {
  padding-left: 30px;
  padding-right: 20px;
}

.row {
  margin-right: -15px;
  margin-left: -15px;
}

#mainstay {
  width: 66.666666666666%;
  float: left;
  padding-right: 15px;
  padding-left: 15px;
}

#sidebar {
  width: 33.333333333333%;
}
body {
  word-wrap: break-word;
}
body {
  font-family: "Microsoft Yahei", Helvetica, Arial, sans-serif;
  font-size: 12px;
  line-height: 1.428571429;
  color: #3d4450;
}
a {
  text-decoration: none;
}

.ct {
  border-radius: 4px;
  box-shadow: -1px -1px 10px rgba(155, 89, 182, 0.5);
  -webkit-box-shadow: -1px -1px 10px rgba(155, 89, 182, 0.5);
  position: relative;
  margin-bottom: 30px;
  padding-bottom: 20px;
  min-height: 20px;
  padding: 19px;
  padding-bottom: 19px;
  margin-bottom: 30px;
  border-radius: 6px;
  background-color: #ffffff;
  -webkit-box-shadow: 0px 0px 10px #9b59b6;
  -moz-box-shadow: 0px 0px 10px #9b59b6;
  box-shadow: 0px 0px 10px #9b59b6;
}

.crinfo-btn {
  text-align: center;
}
</style>

